/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;

/**
 *
 * @author Felipe
 */
public class MessageController {
    
    private static MessageController sMessage = null;
    private final ArrayList<MessageListener> mMessageListeners = new ArrayList<>();
    
    private MessageController() {
        
    }
    
    public void sendMessage(String pMessage) {
        for (int i = 0; i < mMessageListeners.size(); i++) {
            mMessageListeners.get(i).onReceiveMessage(pMessage);
        }
    }
    
    public void sendExceptionMessage(String pMessage, Exception pException) {
        for (int i = 0; i < mMessageListeners.size(); i++) {
            mMessageListeners.get(i).onReceiveExceptionMessage(pMessage, pException);
        }
    }
    
    public void addMessageListener(MessageListener pMessageListener) {
        mMessageListeners.add(pMessageListener);        
    }   
    
    public static MessageController getInstance() {
        return (sMessage == null) ? sMessage = new MessageController() : sMessage;
    }    
    
    public interface MessageListener {

        public void onReceiveMessage(String pMessage);
        public void onReceiveExceptionMessage(String pMessage, Exception pException);
        
    }
    
}